<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                diaryRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    dijit.byId("new_content").setValue("");
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#diaryGird").jqGrid('getGridParam', 'selrow');
                        var diary = jQuery("#diaryGird").jqGrid('getRowData', rowid);
                        dijit.byId('diaryUFD').setValues(diary);
                    }
                };
                topic.subscribe("diary/insertOrUpdate", diaryRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#diaryGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#diaryGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#diaryGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#diaryGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                diarySubmitForm = function(formId, type) {
                    var url = "diary/insert";
                    if (type == 'u') {
                        url = "diary/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("diary/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                diaryDelete = function() {
                    var ids = jQuery("#diaryGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var diary = {};
                            diary.id = '';
                            array.forEach(ids, function(entry, i) {
                                diary.id += entry + ",";
                            });
                            syncDB("diary/delete", JSON.stringify(diary));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                diaryQuery = function(formId) {
                    var diary = dijit.byId(formId).getValues();
                    jQuery("#diaryGird").jqGrid('setGridParam', {
                        postData : diary,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="diaryIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="daytime">日志时间: </label></td>
        <td>
            <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="daytime" class="Wdate" onClick="WdatePicker()">
        </td>
      </tr>
      <tr>
        <td><label for="content">日志内容: </label></td>
        <td>
	        <div name="content" id='new_content' data-dojo-type="dijit/Editor" data-dojo-props="plugins:['bold','italic','|','cut','copy','paste','|','insertUnorderedList']">
	        </div>
        </td>
      </tr>
      <tr>
        <td><label for="diarytype">日志类型：</label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="diarytype" style="width: 100px;">
            <option value="0">个人日志</option>
            <option value="1">工作日志</option>
            </select>
        </td>   
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="diarySubmitForm('diaryIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="diaryRestForm('diaryIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="diaryUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="diaryid" style="display: none;">
    <table>
      <tr>
        <td><label for="daytime">日志时间: </label></td>
        <td>
            <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="daytime" class="Wdate" onClick="WdatePicker()">
        </td>
      </tr>
      <tr>
        <td><label for="content">日志内容: </label></td>
        <td>
            <div name="content" data-dojo-type="dijit/Editor" data-dojo-props="plugins:['bold','italic','|','cut','copy','paste','|','insertUnorderedList']">
            </div>
        </td>
      </tr>
      <tr>
        <td><label for="diarytype">日志类型：</label></td>
        <td>
                <select data-dojo-type="dijit/form/Select" name="diarytype" style="width: 100px;">
                <option value="0">个人日志</option>
                <option value="1">工作日志</option>
                </select>
        </td>   
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="diarySubmitForm('diaryUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="diaryRestForm('diaryUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryDiaryForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
    <table>
        <tr>
            <td>&nbsp;查询条件:&nbsp;日期从:&nbsp;</td>
            <td>
                <input style="width: 100px;" data-dojo-type="dijit/form/TextBox" name="starttime" class="Wdate" id="starttime" onClick="WdatePicker({maxDate:'#F{$dp.$D(\'endtime\')}'})">
            </td>
            <td>&nbsp;到&nbsp;</td>
            <td>
                <input style="width: 100px;" data-dojo-type="dijit/form/TextBox" name="endttime" class="Wdate" id="endtime" onClick="WdatePicker({minDate:'#F{$dp.$D(\'starttime\')}'})">
            </td>
            <td>&nbsp;关键字:</td>
            <td>
                <input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="keyword" style="width: 100px;">
            </td>
            <td>&nbsp;日志类型:</td>  
            <td>
                <select data-dojo-type="dijit/form/Select" name="diarytype" style="width: 100px;">
                <option value="2">全部</option>
                <option value="0">个人日志</option>
                <option value="1">工作日志</option>
                </select>
            </td>           
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="diaryInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('diaryIFD').show();
      </script>
    </button>
    <button id="diaryDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="diaryDelete();"></button>
    <button id="diaryUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#diaryGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var diary = jQuery("#diaryGird").jqGrid('getRowData', ids[0]);
          dijit.byId('diaryUFD').setValues(diary);
          dijit.byId('diaryUFD').show();
        }
      </script>
    </button>
    <button id="diaryQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="diaryQuery('queryDiaryForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="diaryGird"></table>
  <div id="diaryToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#diaryGird").jqGrid({
        url : 'diary/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '日志ID',
          '用户ID',
          '日志时间',
          '日志内容',
          '日志名称',
          '日志类型'
        ],
        colModel : [ 
        {
          name : 'diaryid',
          index : 'diaryid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'userid',
          index : 'userid',
          hidden : true
        },
        {
          name : 'daytime',
          index : 'daytime'
        },
        {
          name : 'content',
          index : 'content',
          width : 600
        },
        {
            //1=工作日志、0=个人日志  
            name : 'diaryname',
            index : 'diaryname'
        },
        {
          //1=工作日志、0=个人日志  
          name : 'diarytype',
          hidden : true,
          index : 'diarytype'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#diaryToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        
        gridComplete : function() {
            var ids = jQuery("#diaryGird").jqGrid("getDataIDs");
            for ( var i = 0; i < ids.length; i++) {
                var cl = ids[i];
                var carcheck = jQuery("#diaryGird").jqGrid("getRowData", cl);
                var status = carcheck['diarytype'] == '1' ? '工作日志' : '个人日志';
                jQuery("#diaryGird").jqGrid("setRowData", cl, {
                    diaryname : status
                }); 
            }
        },
        
        multiselect : true,
        shrinkToFit : false,
        caption : "工作日志列表"
    });
    jQuery("#diaryGird").jqGrid('navGrid', '#diaryToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>

